<?php
// chdir(dirname(__FILE__));
// error_reporting(E_ERROR);
// require_once ("../../config.php");//引入配置
// require_once ("../../log.php");
// require_once ("../api_tool.php");
include "../sdk_jd/JdSdk.php";
/** 
 * @name Api接口demo（php版）
 * @desc 本接口列表，只是系统处理京东联盟的功能
 */

define("AppKey","15c87b8321c1d41ff57125be85da5e3d");
define("AppSecret","eed4fcc9955a4ab6bd05d1ccd8d03b74");

// 京粉精选商品查询接口
function getJdUnionOpenGoodsJingfenQuery($eliteId, $pageIndex) {
	$c = new CpsClient;
	$c->appkey = AppKey;
	$c->secretKey = AppSecret;
	$req = new UnionOpenGoodsJingfeQueryRequest;
	$req->setEliteId($eliteId);
	$req->setPageSize('50');
	$req->setPageIndex($pageIndex);
	$resp = $c->execute($req);
	return $resp;
}

// 关键词商品查询接口【申请】
function getJdUnionOpenGoodsQuery($cid1, $keyword = '', $pageIndex = 1, $pageSize = 30, $sortName = 'commissionShare', $sort = 'desc') {
    $c = new CpsClient;
    $c->appkey = AppKey;
    $c->secretKey = AppSecret;
    $req = new UnionOpenGoodsQueryRequest;
	$req->setCid1($cid1);
	if ($keyword != '') {
		$req->setKeyword($keyword);
	}
    $req->setSortName($sortName);
	$req->setSort($sort);
	$req->setPageIndex($pageIndex);
	$req->setPageSize($pageSize);
    $resp = $c->execute($req);

    return $resp;
}

// 根据skuid查询商品信息接口 (没有数据返回 暂不清楚原因)
function getJdUnionOpenGoodsPromotionGoodsInfoQuery($skuIds) {
	$c = new CpsClient;
	$c->appkey = AppKey;
	$c->secretKey = AppSecret;
	$req = new UnionOpenGoodsPromotiongoodsinfoQueryRequest;
	$req->setSkuIds($skuIds);
	$resp = $c->execute($req);

	return $resp;	
}

// 网站/APP获取推广链接接口
function getJdUnionOpenPromotionCommon($materialId, $siteId, $subUnionId, $couponUrl) {
	$c = new CpsClient;
	$c->appkey = AppKey;
	$c->secretKey = AppSecret;
	$req = new UnionOpenPromotionCommonGetRequest;
	$req->setMaterialId($materialId);
	$req->setSiteId($siteId);
	$req->setSubUnionId($subUnionId);
	$req->setCouponUrl($couponUrl);
	$resp = $c->execute($req);
	return $resp;	
}

// 社交媒体获取推广链接接口【申请】
function getJdunionOpenPromotionBysubunionid($skuId, $subUnionId, $couponUrl = null) {
	$c = new CpsClient;
	$c->appkey = AppKey;
	$c->secretKey = AppSecret;
	$req = new UnionOpenPromotionBysubunionidGetRequest;
	$materialId = "https://wqitem.jd.com/item/view?sku={$skuId}";
	$req->setMaterialId($materialId);
	$req->setSubUnionId($subUnionId);
	$req->setCouponUrl($couponUrl);
	$positionId = explode('_', $subUnionId);
	$req->setPositionId($positionId[1]);
	$req->setChainType("2");
	$resp = $c->execute($req);
	return $resp;
}

// var_dump(getJdunionOpenPromotionBysubunionid("65587677260", "1002283860_9000000003", "https://coupon.m.jd.com/coupons/show.action?key=b7ddf3d171ea41caa206adf3781f85ab&roleId=33791068"));

// 工具商获取推广链接接口【申请】
function getJdUnionOpenPromotionByunionid($skuId, $unionId, $subUnionId, $couponUrl = null) {
	$c = new CpsClient;
	$c->appkey = AppKey;
	$c->secretKey = AppSecret;
	$req = new UnionOpenPromotionBysubunionidGetRequest;
	$materialId = "https://wqitem.jd.com/item/view?sku={$skuId}";
	$req->setMaterialId($materialId);
	$req->setUnionId($unionId);
	$req->setSubUnionId($subUnionId);
	$req->setCouponUrl($couponUrl);
	$req->setChainType("2");
	$resp = $c->execute($req);
	return $resp;
}

// var_dump(getJdUnionOpenGoodsPromotionGoodsInfoQuery(67441364104));
// var_dump(getJdUnionOpenGoodsQuery(12218));

?>